const Goods = require('../model/goods.model')
const { Op } = require('sequelize')
class GoodsService {
    async createGoods(goods) {
        const res = await Goods.create(goods)
        return res.dataValues
    }
    async updateGoods(id, goods) {
        const res = await Goods.update(goods, {where: { id: id }})
        return res[0] > 0
    }
    async findGoods(goodsName, table) {
        let option = {
            limit: table.pageSize,
            offset: table.pageSize * (table.pageNum - 1),
        }
        if(goodsName) {
            option.where = {
                goodsName: {
                    [Op.like]: `%${goodsName}%`
                }
            }
        }
        return {
            data: await Goods.findAll(option),
            total: await Goods.count({ where: option.where || null })
        }
    }
    async changeType(body) {
        const res = await Goods.update({ onShopping: body.onShopping }, {where: { id: body.id }})
        return res[0] > 0
    }
}

module.exports = new GoodsService()